package com.goods.controller.business;

import com.goods.business.service.ProductService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Product;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/business/product")
// business/product/findProductList?pageNum=1&pageSize=6&name=&categoryId=&supplier=&status=0
public class ProductController {

    @Autowired
    private ProductService productService;

    //加载物资列表
    @GetMapping("/findProductList")
    public ResponseBean findProductList(@RequestParam(value = "pageNum" ,required = false) Integer pageNum,
                                        @RequestParam(value = "pageSize",required = false) Integer pageSize,
                                        ProductVO productVO){
        PageVO<SupplierVO>  pageVO =
                productService.findProductList(pageNum,pageSize , productVO);
        return ResponseBean.success(pageVO);
    }

    //add 添加物资
    @PostMapping("/add")
    public ResponseBean add(@RequestBody @Validated ProductVO productVO) throws BusinessException {
        if (productVO.getCategoryKeys().length != 3) {
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"物资需要3级分类");
        }
        productService.add(productVO);
        return ResponseBean.success();
    }

    //编辑
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        ProductVO productVO = productService.edit(id);
        return ResponseBean.success(productVO);
    }
    //添加
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id  ,
                               @RequestBody @Validated  ProductVO productVO){
        productService.update(id,productVO);
        return ResponseBean.success();
    }

    //移入回收站
    @PutMapping("/remove/{id}")
    public ResponseBean remove(@PathVariable Long id) throws BusinessException {
        productService.remove(id);
        return ResponseBean.success();
    }

    //从回收站中回复
    @PutMapping("/back/{id}")
    public ResponseBean back(@PathVariable Long id ){
        productService.back(id);
        return ResponseBean.success();
    }
    //删除
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id) throws BusinessException {
        productService.delete(id);
        return ResponseBean.success();
    }

    //审核
    @PutMapping("/publish/{id}")
    public ResponseBean publish(@PathVariable Long id) throws BusinessException {
        productService.publish(id);
        return ResponseBean.success();
    }
}
